import 'package:flutter/material.dart';

typedef SelectSex<int> = Function(SelectSexType sex);
typedef SelectPictureMethod<int> = Function(SelectPictureType picMethod);
typedef SelectGrade<int> = Function(int grade);

enum SelectPictureType{
  PIC_CAMERA,
  PIC_GALLERY
}

enum SelectSexType{
  MAN,
  WOMAN
}

class DialogUtils{

  /**
   * 隐私协议
   */
  static showPrivacyDialog(BuildContext context,){
    return showDialog(
      context: context,
      builder: (context){
        return AlertDialog(
          title: Text("隐私协议"),
          actionsAlignment: MainAxisAlignment.spaceBetween,
          content: SingleChildScrollView(
            child: Text("我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容我是协议内容"),
          ),
          actions: [
            TextButton(
              onPressed: (){
                Navigator.of(context).pop(false);
              },
              child: Text("不同意"),
            ),
            TextButton(
              onPressed: (){
                Navigator.of(context).pop(true);
              },
              child: Text("同意"),
            ),
          ],
        );
      }
    );
  }

  /**
   * 年级列表
   */
  static showGradeListDialog(BuildContext context,List<Widget> gradeItemList,SelectGrade selectGrade){
    return baseBottomDialog(
        context,
        buildDialogChildren: Expanded(child: CustomScrollView(
          physics: NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          slivers: gradeItemList,
        ))
    );
  }

  /**
   * 选择图片 dialog
   */
  static showBottomPictureDialog(BuildContext context,SelectPictureMethod selectPictureMethod){
    return baseBottomCancelDialog(context,buildDialogChildren:
    Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: (){
            selectPictureMethod(SelectPictureType.PIC_CAMERA);
            Navigator.pop(context);
          },
          child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(left: 10,right: 10),
            padding: EdgeInsets.only(top: 20,bottom: 20),
            child: Text("拍照",style: TextStyle(fontSize: 16,color: Colors.black),),
          ),
        ),
        GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: (){
            selectPictureMethod(SelectPictureType.PIC_GALLERY);
            Navigator.pop(context);
          },
          child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(left: 10,right: 10),
            padding: EdgeInsets.only(top: 20,bottom: 20),
            child: Text("从相册选择",style: TextStyle(fontSize: 16,color: Colors.black),),
          ),
        ),
      ],
    ));
  }

  /**
   * 选择性别 dialog
   */
  static showBottomSexDialog(BuildContext context,SelectSex selectSex){
    return baseBottomCancelDialog(context,buildDialogChildren:
    Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: (){
            selectSex(SelectSexType.MAN);
            Navigator.pop(context);
          },
          child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(left: 10,right: 10),
            padding: EdgeInsets.only(top: 20,bottom: 20),
            child: Text("男",style: TextStyle(fontSize: 16,color: Colors.black),),
          ),
        ),
        GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: (){
            selectSex(SelectSexType.WOMAN);
            Navigator.pop(context);
          },
          child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(left: 10,right: 10),
            padding: EdgeInsets.only(top: 20,bottom: 20),
            child: Text("女",style: TextStyle(fontSize: 16,color: Colors.black),),
          ),
        ),
      ],
    ));
  }

  static baseBottomCancelDialog(BuildContext context,{buildDialogChildren}){
    return baseBottomDialog(context,buildDialogChildren:buildDialogChildren,
        buildCancelButton: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: (){
            Navigator.pop(context);
          },
          child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(left: 20,right: 20,bottom: 20,top: 10),
            padding: EdgeInsets.only(top: 12,bottom: 12),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(30)),
                color: Color(0xFFEEEEED)
            ),
            child: Text(
              "取消",
              style: TextStyle(
                  color: Colors.black,
                  fontSize: 16
              ),
            ),
          ),
        )
    );
  }

  static baseBottomDialog(BuildContext context,{buildDialogChildren,buildCancelButton = const SizedBox(height: 1,)}){
    return showModalBottomSheet<int>(
        context: context,
        backgroundColor: Colors.transparent,
        isScrollControlled: false,
        builder: (context){
          return Container(
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20)),
                  color: Colors.white
              ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.end,
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: [
                  buildDialogChildren,
                  buildCancelButton
                ],
              )
          );
        }
    );
  }
}
